package zozopo.warehouse;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.DatabaseManager;
import zozopo.flow.comm.Flow;
import zozopo.flow.comm.FlowCode;


import zozopo.warehouse.operations.ReceiptOS;
import zozopo.warehouse.operations.ReceiptTB;
import zozopo.warehouse.operations.ReceiptlistOS;
import zozopo.warehouse.operations.ReceiptlistTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class AddReceiptlistAction extends ActionSupport implements Action,SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String limitedCode ="WAREHOUSE.RECEIPTLIST.ADD";
	
	//传入参数
	private int departmentid_receiptlist = 0;
	private String department_receiptlist = "";

	private String customerid_receiptlist = "";
	private String customerinfo_receiptlist = "";

	private double shipprice_receiptlist = 0.00;
	private int goodsnumber_receiptlist = 0;
	private double total_receiptlist = 0;
	private String shipno_receiptlist = "";

	private String remark_receiptlist = "";

	
	private String receiptInfo = "";
	
	//传出参数
	private ReceiptlistTB receiptlist;
	private List<ReceiptTB> receiptList;
	private String operationResult;
	private boolean sqlError = false;
	
	private String lastSalesDate = "";
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	
	
	public String execute(){
		
		///权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operator == null){
			addFieldError("errormessage", "用户未登陆或超时.请登陆后操作.");
			return NONE;
		}
		
		DatabaseManager databaseManager = new BusiDatabaseManager();
		Connection conn = databaseManager.getConnection();
		
	
		try {
			conn.setAutoCommit(false);

			ReceiptOS receiptOS = new ReceiptOS(conn);
			ReceiptlistOS receiptlistOS = new ReceiptlistOS(conn);	
			
			receiptlist = new ReceiptlistTB(); 
			receiptlist.setCustomerid_receiptlist(customerid_receiptlist);
			receiptlist.setCustomerinfo_receiptlist(customerinfo_receiptlist);
			receiptlist.setDepartment_receiptlist(department_receiptlist);
			receiptlist.setDepartmentid_receiptlist(departmentid_receiptlist);
			receiptlist.setGoodsnumber_receiptlist(goodsnumber_receiptlist);
			receiptlist.setRemark_receiptlist(remark_receiptlist);
			receiptlist.setShipprice_receiptlist(shipprice_receiptlist);
			receiptlist.setShipno_receiptlist(shipno_receiptlist);
			
			receiptlist.setTotal_receiptlist(shipprice_receiptlist + total_receiptlist);
			receiptlist.setUseremail_receiptlist(operator.getEmail_operator());
			receiptlist.setUsername_receiptlist(operator.getName_operator()); 
			
			if(!receiptlistOS.insertReceiptlist(receiptlist)){
				sqlError = true;
			}
			
			int maxId = receiptlistOS.getMaxIdByEmail(operator.getEmail_operator());
			receiptlist = receiptlistOS.getReceiptlistById(maxId);
			
			List<ReceiptTB> receiptInfoList = receiptOS.receiptInfoToReceiptList(receiptInfo,receiptlist.getId_receiptlist(),operator);
			
			if(!receiptOS.insertReceipt(receiptInfoList)){
				sqlError = true;
			}
			
			//receiptlistOS.upadteForProfit(receiptlist);
			
			receiptList = receiptOS.getListByReceiptlistId(maxId);
			
			//加入流程
			Flow flow = new Flow(conn);
			if(!flow.executeFlow(FlowCode.WAREHOUSE_RECEIPTLIST, receiptlist.getId_receiptlist(),null, operator, operatorlimitedMap,"")){
				sqlError = true;
			}
			
			
			if(sqlError){
				throw new SQLException();
			}
			
			
			
			conn.commit();
			
			
			ReceiptlistTB lastReceiptlist = receiptlistOS.getLastReceiptlistByCustomerid(receiptlist.getCustomerid_receiptlist());


			if(lastReceiptlist == null){
				lastSalesDate = "新用户!";
			}else{
				lastSalesDate ="上次购买时间:" + lastReceiptlist.getDate_receiptlist() + "";
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			operationResult = "插入失败";
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}catch (Exception e) 
		{
			operationResult = "插入失败";
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			try {
				conn.setAutoCommit(true);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			databaseManager.releaseConnection(conn);
		}
		
		
	
		
		return SUCCESS;//*/
		
	}
	
	public void validate() {
		//权限处理
		customerid_receiptlist = customerid_receiptlist.replaceAll("'", "\\\\'");
		customerinfo_receiptlist = customerinfo_receiptlist.replaceAll("'", "\\\\'");
		remark_receiptlist = remark_receiptlist.replaceAll("'", "\\\\'");
		
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未有出货权限.");
			return;
		}
		
		//用户ID校验
		if(customerid_receiptlist.trim().equals("")){  //为空时处理
			addFieldError("errormessage","客户ID不能为空.");
			return;
		}
		
	}
	




	public String getDepartment_receiptlist() {
		return department_receiptlist;
	}


	public void setDepartment_receiptlist(String department_receiptlist) {
		this.department_receiptlist = department_receiptlist;
	}





	public String getCustomerinfo_receiptlist() {
		return customerinfo_receiptlist;
	}


	public void setCustomerinfo_receiptlist(String customerinfo_receiptlist) {
		this.customerinfo_receiptlist = customerinfo_receiptlist;
	}


	public double getShipprice_receiptlist() {
		return shipprice_receiptlist;
	}


	public void setShipprice_receiptlist(double shipprice_receiptlist) {
		this.shipprice_receiptlist = shipprice_receiptlist;
	}


	public int getGoodsnumber_receiptlist() {
		return goodsnumber_receiptlist;
	}


	public void setGoodsnumber_receiptlist(int goodsnumber_receiptlist) {
		this.goodsnumber_receiptlist = goodsnumber_receiptlist;
	}


	public double getTotal_receiptlist() {
		return total_receiptlist;
	}


	public void setTotal_receiptlist(double total_receiptlist) {
		this.total_receiptlist = total_receiptlist;
	}


	


	public String getRemark_receiptlist() {
		return remark_receiptlist;
	}


	public void setRemark_receiptlist(String remark_receiptlist) {
		this.remark_receiptlist = remark_receiptlist;
	}





	public String getReceiptInfo() {
		return receiptInfo;
	}


	public void setReceiptInfo(String receiptInfo) {
		this.receiptInfo = receiptInfo;
	}


	public ReceiptlistTB getReceiptlist() {
		return receiptlist;
	}


	public void setReceiptlist(ReceiptlistTB receiptlist) {
		this.receiptlist = receiptlist;
	}


	public List<ReceiptTB> getReceiptList() {
		return receiptList;
	}


	public void setReceiptList(List<ReceiptTB> receiptList) {
		this.receiptList = receiptList;
	}


	public int getDepartmentid_receiptlist() {
		return departmentid_receiptlist;
	}


	public void setDepartmentid_receiptlist(int departmentid_receiptlist) {
		this.departmentid_receiptlist = departmentid_receiptlist;
	}


	public String getCustomerid_receiptlist() {
		return customerid_receiptlist;
	}


	public void setCustomerid_receiptlist(String customerid_receiptlist) {
		this.customerid_receiptlist = customerid_receiptlist;
	}


	public String getOperationResult() {
		return operationResult;
	}


	public void setOperationResult(String operationResult) {
		this.operationResult = operationResult;
	}


	public String getLastSalesDate() {
		return lastSalesDate;
	}


	public void setLastSalesDate(String lastSalesDate) {
		this.lastSalesDate = lastSalesDate;
	}


	public String getShipno_receiptlist() {
		return shipno_receiptlist;
	}


	public void setShipno_receiptlist(String shipno_receiptlist) {
		this.shipno_receiptlist = shipno_receiptlist;
	}


	public static String getLimitedcode() {
		return limitedCode;
	}


	

	
}
